package com.rehome.udp;

import javax.swing.*;
import java.awt.*;

/**
 * ip
 * DatagramSocket(hint:port)
 * DatagramPacket(hint:port)
 * send
 * showArea
 */
public class UDPFrame extends JFrame {
    private static JTextField ipField;
    private static JTextField dSocketField;
    private static JTextField dPacketField;
    private static JTextField sendField;
    private static JTextArea textArea;
    private static JButton connect;
    private static JButton transmit;

    public UDPFrame(String title) {
        setSize(700, 480);
        setLocationRelativeTo(null);//居中显示
        setResizable(false);//设置不允许改变窗体大小
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle(title);
    }

    public JTextField getIpField() {
        return ipField;
    }

    public JTextField getDSocketField() {
        return dSocketField;
    }

    public JTextField getDPacketField() {
        return dPacketField;
    }

    public JTextField getSendField() {
        return sendField;
    }

    public JTextArea getTextArea() {
        return textArea;
    }

    public JButton getConnect() {
        return connect;
    }

    public JButton getTransmit() {
        return transmit;
    }

    public static void placeComponents(JPanel panel) {
        panel.setLayout(null);
        Font font1 = new Font("宋体", Font.BOLD, 16);
        Font font2 = new Font("宋体", Font.PLAIN, 16);

        //创建JLabel
        JLabel ip = new JLabel("IP");
        ip.setFont(font1);//设置JLabel的字体
        ip.setBounds(80, 30, 135, 30);
        panel.add(ip);

        JLabel dSocketLabel = new JLabel("DatagramSocket");
        dSocketLabel.setFont(font1);
        dSocketLabel.setBounds(80, 65, 135, 30);
        panel.add(dSocketLabel);

        JLabel dPacketLabel = new JLabel("DatagramPacket");
        dPacketLabel.setFont(font1);
        dPacketLabel.setBounds(80, 100, 135, 30);
        panel.add(dPacketLabel);

        JLabel send = new JLabel("Send");
        send.setFont(font1);
        send.setBounds(80, 135, 135, 30);
        panel.add(send);

        JLabel text = new JLabel("Text");
        text.setFont(font1);
        text.setBounds(80, 170, 135, 30);
        panel.add(text);

        //创建文本域
        ipField = new JTextField(30);
        ipField.setFont(font2);
        ipField.addFocusListener(new ReminderTextAdapter(ipField, "默认：127.0.0.1"));
        ipField.setBounds(220, 30, 360, 30);
        panel.add(ipField);

        dSocketField = new JTextField(30);
        dSocketField.setFont(font2);
        dSocketField.addFocusListener(new ReminderTextAdapter(dSocketField, "port:"));
        dSocketField.setBounds(220, 65, 360, 30);
        panel.add(dSocketField);

        dPacketField = new JTextField(30);
        dPacketField.setFont(font2);
        dPacketField.addFocusListener(new ReminderTextAdapter(dPacketField, "port:"));
        dPacketField.setBounds(220, 100, 360, 30);
        panel.add(dPacketField);

        sendField = new JTextField(30);
        sendField.setFont(font2);
        sendField.addFocusListener(new ReminderTextAdapter(sendField, "请输入要发送的信息:"));
        sendField.setBounds(220, 135, 360, 30);
        panel.add(sendField);

        textArea = new JTextArea();
        textArea.setFont(font2);
        textArea.setBounds(220, 170, 360, 160);
        textArea.setEditable(false);
        textArea.setLineWrap(false);
        panel.add(textArea);

        //给textArea添加滚动条
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(220, 170, 360, 160);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        panel.add(scrollPane);
        scrollPane.setViewportView(textArea);


        //添加两个按钮
        connect = new JButton("连接");
        connect.setFont(font1);
        connect.setBounds(140, 360, 115, 40);
        panel.add(connect);

        transmit = new JButton("发送");
        transmit.setFont(font1);
        transmit.setBounds(440, 360, 115, 40);
        panel.add(transmit);
    }
}

